package com.gz.logistics.app;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gz.logistics.R;
import com.gz.logistics.adapter.SpinnerAdapter;
import com.gz.logistics.fragment.HomeFragment;
import com.gz.logistics.library.IOrder;
import com.gz.logistics.library.listener.GetClothesBugListener;
import com.gz.logistics.library.listener.GetClothesCategoryListener;
import com.gz.logistics.library.listener.GetClothesColorListener;
import com.gz.logistics.library.listener.GetClothesNameListener;
import com.gz.logistics.library.listener.GetClothesPinpaiListener;
import com.gz.logistics.library.listener.GetClothesSpecialListener;
import com.gz.logistics.library.listener.PostClothesInfoListener;
import com.gz.logistics.library.listener.PostMoneyListener;
import com.gz.logistics.library.pojo.ClothesBug;
import com.gz.logistics.library.pojo.ClothesCategory;
import com.gz.logistics.library.pojo.ClothesColor;
import com.gz.logistics.library.pojo.ClothesName;
import com.gz.logistics.library.pojo.ClothesPinpai;
import com.gz.logistics.library.pojo.ClothesSpecial;
import com.gz.logistics.library.pojo.CustomerSpinnerObject;
import com.gz.logistics.library.util.CrashHandler;
import com.gz.logistics.util.ToastUtils;
import com.gz.logistics.widget.DoubleDatePickerDialogOne;
import com.gz.logistics.widget.SpinerPopWindow;

import java.lang.ref.WeakReference;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 签收选择用户的支付方式确定信息
 * 杜志娟
 * 2016-02-26
 */
public class ApplyclothesinfoActivity extends BaseActivity implements View.OnClickListener,DoubleDatePickerDialogOne.OnDateSetListener{
    private String TAG = ApplyclothesinfoActivity.class.getSimpleName();
    private List<CustomerSpinnerObject> clothescategory;//衣物分类
    private List<CustomerSpinnerObject> clothesname;//衣物名称
    private List<CustomerSpinnerObject> clothescolor;//衣物颜色
    private List<CustomerSpinnerObject> clothespinpai;//衣物品牌
    private List<CustomerSpinnerObject> clothesbugs;//衣物瑕疵
    private List<CustomerSpinnerObject> clothesSpecial;//衣物特殊处理

    private RelativeLayout rl_clothes_bigtype;//衣物种类
    private RelativeLayout rl_clothes_littletype;//衣物名称
    private RelativeLayout rl_clothes_colors;//衣物颜色
    private RelativeLayout rl_clothes_pinpai;//衣物的品牌
    private RelativeLayout rl_clothes_teshu;//特殊处理
    private RelativeLayout rl_clothes_xiaci;//存在瑕疵

    private SpinerPopWindow mclothescategroyPopwindow;
    private SpinerPopWindow mclothesnamePopwindow;
    private SpinerPopWindow mclothescolorPopwindow;
    private SpinerPopWindow mclothespinpaiPopwindow;
    private SpinerPopWindow mclothesspecialPopwindow;
    private SpinerPopWindow mclothesbugPopwindow;

    private SpinnerAdapter mclothescategroySpinnerAdapter;
    private SpinnerAdapter mclothesnameSpinnerAdapter;
    private SpinnerAdapter mclothescolorSpinnerAdapter;
    private SpinnerAdapter mclothespinpaiSpinnerAdapter;
    private SpinnerAdapter mclothesspecialSpinnerAdapter;
    private SpinnerAdapter mclothesbugSpinnerAdapter;

    private MyClothesCategoryOnItemSelectListener myClothesCategoryOnItemSelectListener;
    private MyClothesNameOnItenSelectListener myClothesNameOnItemSelectListener;
    private MyClothesCloorOnItemSelectListener myClothesColorOnItemSelectListener;
    private MyClothespinpaiOnItemSelectListener myClothespinpaiOnItemSelectListener;
    private MyClothesSpecialOnItemSelectListener myClothesSpecialOnItemSelectListener;
    private MyClothesbugOnItemSelectListener myClothesbugOnItemselectListener;

    private myGetClothesCategory getClothesCategorylistener;
    private myGetClothesName getClothesNameListener;
    private myGerClothesColor getClothesColorListener;
    private myGerClothespinpai getClothespinpaiListener;
    private myGetClothesbug getClothesbugListener;
    private myGetClothesSpecial getClothesSpecialListener;

    private PostClothesInfoListener postClothesInfoListener;
    private MyHandler mHandler;
    private Dialog mMenudialog;
    private TextView mTvTitle;
    private RelativeLayout btn_ok;
    private EditText edittext;
    private Dialog mLoadingData;
    private Button apply_info;//提交按钮
    private EditText tiaoxing_num;//条形码
    private TextView yuyue_time;//预约时间
    private DoubleDatePickerDialogOne timeDialog;
    private Calendar mCalendar;
    private Button apply_ok;//完成按钮

    private int clothes_category_id = 0;//衣物种类的id
    private int name_id = 0;//衣物名称
    private String color = "";//衣物颜色
    private String style = "";//衣物品牌
    private String order_no = "";//预约单号
    private double countmoney= 0;//收衣完成后应收的总金额

    private TextView tv_clothestype;//衣物种类
    private TextView tv_clothesname;//衣物名称
    private TextView tv_clothescolor;//衣物颜色
    private TextView tv_clothespinpai;//衣物品牌
    private TextView tv_clothesbug;//衣物瑕疵
    private TextView tv_clothesteshu;//特殊处理
    private TextView count_money;//应收金额
    private TextView special_money;//特殊处理费
    private ImageView iv_actionbar_QRCoder;//扫一扫
    private TextView show_tv;
    private PostMoneyListener postmoneyListener;//收款监听
    private TextView special_content;//特殊处理的备注内容
    private TextView bug_content;//存在瑕疵的备注内容
    private HashMap<String, String> hashMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clothesproperty);
        setActionbarTitle("提交详情", true);
        init();

    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_clothes_bigtype:
                if (clothescategory.size() <= 0){
                    ToastUtils.showToast(this, "衣物种类列表为空");
                }
                mclothescategroyPopwindow.setWidth(rl_clothes_bigtype.getWidth()*2);
                mclothescategroyPopwindow.showAsDropDown(rl_clothes_bigtype);
                break;

            case R.id.rl_clothes_littletype:
                if (clothesname.size() <= 0){
                    ToastUtils.showToast(this, "衣物名称列表为空");
                }
                mclothesnamePopwindow.setWidth(rl_clothes_littletype.getWidth()*2);
                mclothesnamePopwindow.showAsDropDown(rl_clothes_littletype);
                break;
            case R.id.rl_clothes_colors:
                if (clothescolor.size() <= 0){
                    ToastUtils.showToast(this, "衣物颜色列表为空");
                }
                mclothescolorPopwindow.setWidth(rl_clothes_colors.getWidth());
                mclothescolorPopwindow.showAsDropDown(rl_clothes_colors);
                break;
            case R.id.rl_clothes_pinpai:
                if (clothespinpai.size() <= 0){
                    ToastUtils.showToast(this, "衣物品牌列表为空");
                }
                mclothespinpaiPopwindow.setWidth(rl_clothes_pinpai.getWidth()*2);
                mclothespinpaiPopwindow.showAsDropDown(rl_clothes_pinpai);
                break;
            case R.id.rl_clothes_xiaci:
                if (clothesbugs.size() <= 0){
                    ToastUtils.showToast(this, "衣物瑕疵列表为空");
                }
                mclothesbugPopwindow.setWidth(rl_clothes_xiaci.getWidth());
                mclothesbugPopwindow.showAsDropDown(rl_clothes_xiaci);
                break;
            case R.id.rl_clothes_teshu:
                if (clothesSpecial.size() <= 0){
                    ToastUtils.showToast(this, "衣物特殊处理列表为空");
                }
                mclothesspecialPopwindow.setWidth(rl_clothes_teshu.getWidth());
                mclothesspecialPopwindow.showAsDropDown(rl_clothes_teshu);
                break;
            case R.id.apply_info:
                if(clothes_category_id==0){
                    ToastUtils.showToast(getApplicationContext(), "请选择衣物种类", Toast.LENGTH_SHORT);

                }else if(name_id==0){
                    ToastUtils.showToast(getApplicationContext(), "请选择衣物名称", Toast.LENGTH_SHORT);
                }else if(tiaoxing_num.getText().toString().trim()==null || tiaoxing_num.getText().toString().trim().equals("")){
                    ToastUtils.showToast(getApplicationContext(), "请输入有效条形码", Toast.LENGTH_SHORT);
                }
                else{
                    mLoadingData.show();
                    IOrder.getInstance().PostClothesInfo(clothes_category_id,Double.parseDouble(count_money.getText().toString()),name_id,color,style,bug_content.getText().toString().trim(),special_content.getText().toString().trim(),tiaoxing_num.getText().toString().trim(),
                            order_no,yuyue_time.getText().toString().trim(),Double.parseDouble(special_money.getText().toString()),postClothesInfoListener);
                }
                break;
            case R.id.yuyue_time:
                timeDialog.show();
                break;
            case R.id.apply_ok://点击完成按钮完成此订单的收取衣服
                if(countmoney==0){
                    finish();
                }else{
                    createDialogMoney(order_no, countmoney);
                }
                break;
            case R.id.iv_actionbar_QRCoder:
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(),MipcaActivityCapture.class);
                startActivityForResult(intent, 100);
                break;

        }
    }
    /**
     * 数据回调
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 100:
                String result = data.getStringExtra("result");
                tiaoxing_num.setText(result);
                break;
        }
    }

    /**
     * 数据初始化
     */
    public void init(){
        rl_clothes_bigtype = (RelativeLayout)findViewById(R.id.rl_clothes_bigtype);
        rl_clothes_bigtype.setOnClickListener(this);
        rl_clothes_littletype = (RelativeLayout)findViewById(R.id.rl_clothes_littletype);
        rl_clothes_littletype.setOnClickListener(this);
        rl_clothes_colors = (RelativeLayout)findViewById(R.id.rl_clothes_colors);
        rl_clothes_colors.setOnClickListener(this);
        rl_clothes_pinpai = (RelativeLayout)findViewById(R.id.rl_clothes_pinpai);
        rl_clothes_pinpai.setOnClickListener(this);
        rl_clothes_teshu = (RelativeLayout)findViewById(R.id.rl_clothes_teshu);
        rl_clothes_teshu.setOnClickListener(this);
        rl_clothes_xiaci = (RelativeLayout)findViewById(R.id.rl_clothes_xiaci);
        rl_clothes_xiaci.setOnClickListener(this);
        apply_info = (Button)findViewById(R.id.apply_info);
        apply_info.setOnClickListener(this);
        tiaoxing_num = (EditText)findViewById(R.id.tiaoxing_num);
        yuyue_time = (TextView)findViewById(R.id.yuyue_time);
        yuyue_time.setOnClickListener(this);
        apply_ok = (Button)findViewById(R.id.apply_ok);
        apply_ok.setOnClickListener(this);
        tv_clothestype = (TextView)findViewById(R.id.tv_clothestype);//衣物种类
        tv_clothesname = (TextView)findViewById(R.id.tv_clothesname);//衣物名称
        tv_clothescolor = (TextView)findViewById(R.id.tv_clothescolor);//衣物颜色
        tv_clothespinpai = (TextView)findViewById(R.id.tv_clothespinpai);//衣物品牌
        tv_clothesteshu = (TextView)findViewById(R.id.tv_clothesteshu);//特殊处理
        tv_clothesbug = (TextView)findViewById(R.id.tv_clothesbug);//衣物瑕疵
        count_money = (TextView)findViewById(R.id.count_money);//应收金额
        special_money = (TextView)findViewById(R.id.special_money);//特殊处理费
        iv_actionbar_QRCoder = (ImageView)findViewById(R.id.iv_actionbar_QRCoder);
        iv_actionbar_QRCoder.setOnClickListener(this);
        special_content = (TextView) findViewById(R.id.spetial_content);
        bug_content = (TextView) findViewById(R.id.bug_content);

        order_no = getIntent().getStringExtra("order_no");
        mCalendar = Calendar.getInstance();
        timeDialog = new DoubleDatePickerDialogOne(this,0,this,mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DATE),true,mCalendar.get(Calendar.HOUR),mCalendar.get(Calendar.MINUTE));

        clothescategory = new ArrayList<CustomerSpinnerObject>();
        clothesname = new ArrayList<CustomerSpinnerObject>();
        clothescolor = new ArrayList<CustomerSpinnerObject>();
        clothespinpai = new ArrayList<CustomerSpinnerObject>();
        clothesbugs = new ArrayList<CustomerSpinnerObject>();
        clothesSpecial = new ArrayList<CustomerSpinnerObject>();
        hashMap = new HashMap<String, String>();

        mclothescategroySpinnerAdapter = new SpinnerAdapter(getApplicationContext(),clothescategory);
        mclothescategroyPopwindow = new SpinerPopWindow(getApplicationContext());
        mclothescategroyPopwindow.setAdatper(mclothescategroySpinnerAdapter);
        myClothesCategoryOnItemSelectListener = new MyClothesCategoryOnItemSelectListener(this);
        mclothescategroyPopwindow.setItemListener(myClothesCategoryOnItemSelectListener);

        mclothesnameSpinnerAdapter = new SpinnerAdapter(getApplicationContext(),clothesname);
        mclothesnamePopwindow = new SpinerPopWindow(getApplicationContext());
        mclothesnamePopwindow.setAdatper(mclothesnameSpinnerAdapter);
        myClothesNameOnItemSelectListener = new MyClothesNameOnItenSelectListener(this);
        mclothesnamePopwindow.setItemListener(myClothesNameOnItemSelectListener);

        mclothescolorSpinnerAdapter = new SpinnerAdapter(getApplicationContext(),clothescolor);
        mclothescolorPopwindow = new SpinerPopWindow(getApplicationContext());
        mclothescolorPopwindow.setAdatper(mclothescolorSpinnerAdapter);
        myClothesColorOnItemSelectListener = new MyClothesCloorOnItemSelectListener(this);
        mclothescolorPopwindow.setItemListener(myClothesColorOnItemSelectListener);

        mclothespinpaiSpinnerAdapter = new SpinnerAdapter(getApplicationContext(),clothespinpai);
        mclothespinpaiPopwindow = new SpinerPopWindow(getApplicationContext());
        mclothespinpaiPopwindow.setAdatper(mclothespinpaiSpinnerAdapter);
        myClothespinpaiOnItemSelectListener = new MyClothespinpaiOnItemSelectListener(this);
        mclothespinpaiPopwindow.setItemListener(myClothespinpaiOnItemSelectListener);

        mclothesbugSpinnerAdapter = new SpinnerAdapter(getApplicationContext(),clothesbugs);
        mclothesbugPopwindow = new SpinerPopWindow(getApplicationContext());
        mclothesbugPopwindow.setAdatper(mclothesbugSpinnerAdapter);
        myClothesbugOnItemselectListener = new MyClothesbugOnItemSelectListener(this);
        mclothesbugPopwindow.setItemListener(myClothesbugOnItemselectListener);

        mclothesspecialSpinnerAdapter = new SpinnerAdapter(getApplicationContext(),clothesSpecial);
        mclothesspecialPopwindow = new SpinerPopWindow(getApplicationContext());
        mclothesspecialPopwindow.setAdatper(mclothesspecialSpinnerAdapter);
        myClothesSpecialOnItemSelectListener = new MyClothesSpecialOnItemSelectListener(this);
        mclothesspecialPopwindow.setItemListener(myClothesSpecialOnItemSelectListener);

        getClothesCategorylistener = new myGetClothesCategory(this);
        getClothesNameListener = new myGetClothesName(this);
        getClothesColorListener = new myGerClothesColor(this);
        getClothespinpaiListener = new myGerClothespinpai(this);
        getClothesbugListener = new myGetClothesbug(this);
        getClothesSpecialListener = new myGetClothesSpecial(this);

        mLoadingData = new Dialog(this, R.style.DialogTheme);
        mLoadingData.setContentView(R.layout.loading_data_dialog2);
        mLoadingData.setCancelable(false);
        mLoadingData.setCanceledOnTouchOutside(false);
        show_tv = (TextView)mLoadingData.findViewById(R.id.show_tv);

        initListener();

        mHandler = new MyHandler(this);
        mHandler.sendEmptyMessage(1);
        mHandler.sendEmptyMessage(2);
        mHandler.sendEmptyMessage(3);
        mHandler.sendEmptyMessage(4);
        mHandler.sendEmptyMessage(5);

    }

    public static class MyHandler extends android.os.Handler{
        private ApplyclothesinfoActivity mOuter;

        public MyHandler(ApplyclothesinfoActivity mOuter) {
            this.mOuter = mOuter;
        }
        public void release(){
            new WeakReference<ApplyclothesinfoActivity>(mOuter);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    IOrder.getInstance().getClothesCategory(mOuter.getClothesCategorylistener);
                    break;
                case 2:
                    IOrder.getInstance().getClothesColor(mOuter.getClothesColorListener);
                    break;
                case 3:
                    IOrder.getInstance().getClothesPinpai(mOuter.getClothespinpaiListener);
                    break;
                case 4:
                    IOrder.getInstance().getClothesBugs(mOuter.getClothesbugListener);
                    break;
                case 5:
                    IOrder.getInstance().getClothesSpecial(mOuter.getClothesSpecialListener);
                    break;
            }
        }
    }
    public void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth,int hours,int minutes) {
        yuyue_time.setText(String.format("%d-%d-%d %d:%d", startYear, startMonthOfYear + 1, startDayOfMonth,hours,minutes));
    }
    // 关闭程序掉用处理部分
    public void onDestroy() {
        super.onDestroy();
        mHandler.release();
        getClothesCategorylistener.release();
        myClothesCategoryOnItemSelectListener.release();
        getClothesNameListener.release();
        myClothesNameOnItemSelectListener.release();
        getClothesColorListener.release();
        myClothesColorOnItemSelectListener.release();
        getClothespinpaiListener.release();
        myClothespinpaiOnItemSelectListener.release();
    }
    public static class MyClothesCategoryOnItemSelectListener implements SpinerPopWindow.ISpinnerOnItemSelectListener {
        private ApplyclothesinfoActivity mOuter;

        public MyClothesCategoryOnItemSelectListener(ApplyclothesinfoActivity mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<ApplyclothesinfoActivity>(mOuter);
        }
        @Override
        public void onSpinnerItemClick(int position) {
            mOuter.clothesname.clear();
            mOuter.clothes_category_id = 0;
            mOuter.clothes_category_id = ((ClothesCategory)mOuter.clothescategory.get(position)).getId();
            mOuter.tv_clothestype.setText(((ClothesCategory)mOuter.clothescategory.get(position)).getClothingcategory());
            for(int j = 0 ; j < mOuter.clothescategory.size() ; j++){
                if(j == position){
                    (mOuter.clothescategory.get(j)).setIsSelected(true);
                    IOrder.getInstance().getClothesName(mOuter.clothes_category_id, mOuter.getClothesNameListener);
                }else{
                    (mOuter.clothescategory.get(j)).setIsSelected(false);
                }
            }

        }
    }
    public static class MyClothesNameOnItenSelectListener implements SpinerPopWindow.ISpinnerOnItemSelectListener {
        private ApplyclothesinfoActivity mOuter;

        public MyClothesNameOnItenSelectListener(ApplyclothesinfoActivity mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<ApplyclothesinfoActivity>(mOuter);
        }
        @Override
        public void onSpinnerItemClick(int position) {
            mOuter.name_id = 0;
            mOuter.name_id = ((ClothesName)mOuter.clothesname.get(position)).getId();
            if (((ClothesName)mOuter.clothesname.get(position)).getClothesname().length()>4){
                mOuter.tv_clothesname.setText(((ClothesName)mOuter.clothesname.get(position)).getClothesname().substring(0,4)+"...");
            }else{
                mOuter.tv_clothesname.setText(((ClothesName)mOuter.clothesname.get(position)).getClothesname());
            }
            mOuter.count_money.setText(((ClothesName)mOuter.clothesname.get(position)).getPrice());
            Calendar c=Calendar.getInstance();
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            c.setTime(new Date());
            c.add(Calendar.DATE,((ClothesName)mOuter.clothesname.get(position)).getDeliveryday());
            Date d2=c.getTime();
            mOuter.yuyue_time.setText(df.format(d2));
            for(int j = 0 ; j < mOuter.clothesname.size() ; j++){ // 把选中的仓库置为选中状态，
                if(j == position){
                    (mOuter.clothesname.get(j)).setIsSelected(true);
                }else{
                    (mOuter.clothesname.get(j)).setIsSelected(false);
                }
            }
        }
    }
    public static class MyClothespinpaiOnItemSelectListener implements SpinerPopWindow.ISpinnerOnItemSelectListener {
        private ApplyclothesinfoActivity mOuter;

        public MyClothespinpaiOnItemSelectListener(ApplyclothesinfoActivity mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<ApplyclothesinfoActivity>(mOuter);
        }
        @Override
        public void onSpinnerItemClick(int position) {
            mOuter.style = ((ClothesPinpai)mOuter.clothespinpai.get(position)).getBrand();
            if(((ClothesPinpai)mOuter.clothespinpai.get(position)).getId()==0){
                mOuter.tv_clothespinpai.setText("品牌");
                mOuter.style = "";
            }else{
                mOuter.tv_clothespinpai.setText(((ClothesPinpai)mOuter.clothespinpai.get(position)).getBrand());
            }
            for(int j = 0 ; j < mOuter.clothespinpai.size() ; j++){
                if(j == position){
                    (mOuter.clothespinpai.get(j)).setIsSelected(true);
                }else{
                    (mOuter.clothespinpai.get(j)).setIsSelected(false);
                }
            }

        }
    }
    public static class MyClothesCloorOnItemSelectListener implements SpinerPopWindow.ISpinnerOnItemSelectListener {
        private ApplyclothesinfoActivity mOuter;

        public MyClothesCloorOnItemSelectListener(ApplyclothesinfoActivity mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<ApplyclothesinfoActivity>(mOuter);
        }
        @Override
        public void onSpinnerItemClick(int position) {
            mOuter.color = ((ClothesColor)mOuter.clothescolor.get(position)).getColor();
            if(((ClothesColor)mOuter.clothescolor.get(position)).getId()==0){
                mOuter.tv_clothescolor.setText("颜色");
                mOuter.color = "";
            }else{
                mOuter.tv_clothescolor.setText(((ClothesColor)mOuter.clothescolor.get(position)).getColor());
            }
            for(int j = 0 ; j < mOuter.clothescolor.size() ; j++){
                if(j == position){
                    (mOuter.clothescolor.get(j)).setIsSelected(true);
                }else{
                    (mOuter.clothescolor.get(j)).setIsSelected(false);
                }
            }

        }
    }

    public static class MyClothesbugOnItemSelectListener implements SpinerPopWindow.ISpinnerOnItemSelectListener {
        private ApplyclothesinfoActivity mOuter;

        public MyClothesbugOnItemSelectListener(ApplyclothesinfoActivity mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<ApplyclothesinfoActivity>(mOuter);
        }
        @Override
        public void onSpinnerItemClick(int position) {

            if (((ClothesBug)mOuter.clothesbugs.get(position)).getId()==0){
                mOuter.bug_content.setText("");
                mOuter.tv_clothesbug.setText("请选择");
            }else{
                if (isContain(mOuter.bug_content.getText().toString()," "+((ClothesBug)mOuter.clothesbugs.get(position)).getFlawsnote())==true){//判断是否存才已选瑕疵
                    mOuter.bug_content.setText(mOuter.bug_content.getText().toString().replace(" " + ((ClothesBug) mOuter.clothesbugs.get(position)).getFlawsnote(), ""));//有则删除
                    mOuter.tv_clothesbug.setText("请选择");
                }else{
                    mOuter.bug_content.setText(mOuter.bug_content.getText().toString().concat((" "+((ClothesBug)mOuter.clothesbugs.get(position)).getFlawsnote())));//追加信息
                    mOuter.tv_clothesbug.setText(((ClothesBug)mOuter.clothesbugs.get(position)).getFlawsnote());
                }
            }
            for(int j = 0 ; j < mOuter.clothesbugs.size() ; j++){
                if(j == position){
                    (mOuter.clothesbugs.get(j)).setIsSelected(true);
                }else{
                    (mOuter.clothesbugs.get(j)).setIsSelected(false);
                }
            }

        }
    }
    public static class MyClothesSpecialOnItemSelectListener implements SpinerPopWindow.ISpinnerOnItemSelectListener {
        private ApplyclothesinfoActivity mOuter;

        public MyClothesSpecialOnItemSelectListener(ApplyclothesinfoActivity mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<ApplyclothesinfoActivity>(mOuter);
        }
        @Override
        public void onSpinnerItemClick(int position) {

            if (((ClothesSpecial)mOuter.clothesSpecial.get(position)).getId()==0){
                mOuter.tv_clothesteshu.setText("请选择");
                mOuter.special_content.setText("");
            }else{
                if (isContain(mOuter.special_content.getText().toString()," "+((ClothesSpecial)mOuter.clothesSpecial.get(position)).getProcess())==true){//判断是否存才已选特殊处理
                    String content = mOuter.hashMap.get(((ClothesSpecial) mOuter.clothesSpecial.get(position)).getProcess());
                    mOuter.special_money.setText(Double.parseDouble(mOuter.special_money.getText().toString()) - Double.parseDouble(content.substring(content.indexOf("-")+1, content.indexOf("元")))+"");
                    mOuter.special_content.setText(mOuter.special_content.getText().toString().replace(mOuter.hashMap.get(((ClothesSpecial) mOuter.clothesSpecial.get(position)).getProcess()), ""));//有则删除
                    mOuter.tv_clothesteshu.setText("请选择");
                }else{
                    mOuter.createDialog(((ClothesSpecial) mOuter.clothesSpecial.get(position)).getProcess());
                }

            }
            for(int j = 0 ; j < mOuter.clothesSpecial.size() ; j++){
                if(j == position){
                    (mOuter.clothesSpecial.get(j)).setIsSelected(true);
                }else{
                    (mOuter.clothesSpecial.get(j)).setIsSelected(false);
                }

            }

        }
    }


    /**
     * 获取衣物种类列表
     */
    public static class myGetClothesCategory extends GetClothesCategoryListener {
        private ApplyclothesinfoActivity mOuter;

        public myGetClothesCategory(ApplyclothesinfoActivity mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<ApplyclothesinfoActivity>(mOuter);
        }

        @Override
        public void onResponseSuccess(List<ClothesCategory> categoryList) {
            mOuter.clothescategory.clear();
            mOuter.clothescategory.addAll(categoryList);
        }
        @Override
        public void onNoMoreData() {

        }
        @Override
        public void onResponseFailed() {

        }
    }
    /**
     * 获取衣物名称列表
     */
    public static class myGetClothesName extends GetClothesNameListener {
        private ApplyclothesinfoActivity mOuter;

        public myGetClothesName(ApplyclothesinfoActivity mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<ApplyclothesinfoActivity>(mOuter);
        }

        @Override
        public void onResponseSuccess(List<ClothesName> nameList) {
            nameList.get(0).setIsSelected(true);
            mOuter.clothesname.clear();
            mOuter.clothesname.addAll(nameList);
        }
        @Override
        public void onNoMoreData() {

        }
        @Override
        public void onResponseFailed() {

        }
    }
    /**
     * 获取衣物颜色列表
     */
    public static class myGerClothesColor extends GetClothesColorListener {
        private ApplyclothesinfoActivity mOuter;

        public myGerClothesColor(ApplyclothesinfoActivity mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<ApplyclothesinfoActivity>(mOuter);
        }

        @Override
        public void onResponseSuccess(List<ClothesColor> colorList) {
            ClothesColor color = new ClothesColor();
            color.setId(0);
            color.setColor("取消");
            color.setIsSelected(true);
            colorList.add(0, color);
            mOuter.clothescolor.clear();
            mOuter.clothescolor.addAll(colorList);
        }
        @Override
        public void onNoMoreData() {

        }
        @Override
        public void onResponseFailed() {

        }
    }
    /**
     * 获取衣物品牌列表
     */
    public static class myGerClothespinpai extends GetClothesPinpaiListener {
        private ApplyclothesinfoActivity mOuter;

        public myGerClothespinpai(ApplyclothesinfoActivity mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<ApplyclothesinfoActivity>(mOuter);
        }

        @Override
        public void onResponseSuccess(List<ClothesPinpai> pinpaiList) {
            ClothesPinpai pinpai = new ClothesPinpai();
            pinpai.setId(0);
            pinpai.setBrand("取消");
            pinpai.setIsSelected(true);
            pinpaiList.add(0, pinpai);
            mOuter.clothespinpai.clear();
            mOuter.clothespinpai.addAll(pinpaiList);
        }
        @Override
        public void onNoMoreData() {

        }
        @Override
        public void onResponseFailed() {

        }
    }
    /**
     * 获取衣物瑕疵列表
     */
    public static class myGetClothesbug extends GetClothesBugListener {
        private ApplyclothesinfoActivity mOuter;

        public myGetClothesbug(ApplyclothesinfoActivity mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<ApplyclothesinfoActivity>(mOuter);
        }

        @Override
        public void onResponseSuccess(List<ClothesBug> buglist) {
            ClothesBug info = new ClothesBug();
            info.setId(0);
            info.setFlawsnote("取消");
            info.setIsSelected(true);
            buglist.add(0, info);
            mOuter.clothesbugs.clear();
            mOuter.clothesbugs.addAll(buglist);
        }
        @Override
        public void onNoMoreData() {

        }
        @Override
        public void onResponseFailed() {

        }
    }
    /**
     * 获取特殊处理列表
     */
    public static class myGetClothesSpecial extends GetClothesSpecialListener {
        private ApplyclothesinfoActivity mOuter;

        public myGetClothesSpecial(ApplyclothesinfoActivity mOuter) {
            this.mOuter = mOuter;
        }

        public void release(){
            new WeakReference<ApplyclothesinfoActivity>(mOuter);
        }

        @Override
        public void onResponseSuccess(List<ClothesSpecial> specail) {
            ClothesSpecial info = new ClothesSpecial();
            info.setId(0);
            info.setProcess("取消");
            info.setIsSelected(true);
            specail.add(0, info);
            mOuter.clothesSpecial.clear();
            mOuter.clothesSpecial.addAll(specail);

        }
        @Override
        public void onNoMoreData() {

        }
        @Override
        public void onResponseFailed() {

        }
    }

    private void createDialog(final String special_title) {
        mMenudialog = new Dialog(this, R.style.DialogTheme);
        View dialogView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.mycommunity_dialog_layout, null);
        mTvTitle = (TextView) dialogView.findViewById(R.id.tv_title);
        mTvTitle.setText("请输入金额：");
        edittext = (EditText) dialogView.findViewById(R.id.money);
        btn_ok = (RelativeLayout) dialogView.findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                special_content.setText(special_content.getText().toString().concat((" " + special_title+"-" + edittext.getText().toString().trim() + "元")));//追加信息
                tv_clothesteshu.setText(special_title);
                if (edittext.getText().toString().trim().equals("")){
                    edittext.setText("0");
                }
                special_money.setText(Double.parseDouble(edittext.getText().toString().trim())+Double.parseDouble(special_money.getText().toString().trim())+"");
                hashMap.put(special_title,(" " + special_title+"-" + edittext.getText().toString().trim() + "元"));
                mMenudialog.dismiss();
            }
        });
        dialogView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mMenudialog.dismiss();
            }
        });
        mMenudialog.setContentView(dialogView);
        mMenudialog.setCancelable(false);
        mMenudialog.setCanceledOnTouchOutside(false);
        mMenudialog.show();
    }



    private void initListener() {

        postClothesInfoListener = new PostClothesInfoListener() {

            @Override
            public void onResponseSuccess() {
                ToastUtils.showToast(getApplicationContext(), "提交成功！", Toast.LENGTH_SHORT);
                countmoney = countmoney + Double.parseDouble(count_money.getText().toString()) +Double.parseDouble(special_money.getText().toString());//计算应收总额
                mLoadingData.dismiss();
                name_id = 0;//衣物名称
                color = "";//衣物颜色
                style = "";//衣物品牌
                bug_content.setText("");//衣物瑕疵
                special_content.setText("");//衣物特殊处理
                yuyue_time.setText("");
                tiaoxing_num.setText("");
                tv_clothestype.setText("衣物处理");
                tv_clothesname.setText("名称");
                tv_clothescolor.setText("颜色");
                tv_clothespinpai.setText("品牌");
                tv_clothesteshu.setText("请选择");
                tv_clothesbug.setText("请选择");
                count_money.setText("0");
                special_money.setText("0");
                hashMap = new HashMap<String, String>();

            }

            @Override
            public void onResponseFailed() {
                // TODO Auto-generated method stub
                ToastUtils.showToast(getApplicationContext(), "提交失败, 请重试", Toast.LENGTH_SHORT);
                mLoadingData.dismiss();
            }

            @Override
            public void onOffline() {//处于离线状态
                // TODO Auto-generated method stub
                mLoadingData.dismiss();
            }

            @Override
            public void onNoMoreData() {
                ToastUtils.showToast(getApplicationContext(), "标签号重复，请重新确认", Toast.LENGTH_SHORT);
                mLoadingData.dismiss();
            }
            @Override
            public void onNoNetWork() {//没有任何网络连接
                // TODO Auto-generated method stub
                ToastUtils.showToast(getApplicationContext(),"此订单已失效" ,Toast.LENGTH_SHORT);
                mLoadingData.dismiss();
            }
        };

        postmoneyListener = new PostMoneyListener() {

            @Override
            public void onResponseSuccess() {
                ToastUtils.showToast(getApplicationContext(), "收款成功！", Toast.LENGTH_SHORT);
                mLoadingData.dismiss();
                Intent broastIntent = new Intent(Define.ACTION_REFRESH_CLOTHESLIST);//发送广播刷新列表
                sendBroadcast(broastIntent);
                finish();
            }

            @Override
            public void onOffline() {//处于离线状态
                // TODO Auto-generated method stub
                mLoadingData.dismiss();
            }

            @Override
            public void onResponseFailed() {
                ToastUtils.showToast(getApplicationContext(), "收款失败,请重试", Toast.LENGTH_SHORT);
                mLoadingData.dismiss();
            }

            @Override
            public void onNoMoreData() {
                mLoadingData.dismiss();
            }

            @Override
            public void onNoNetWork() {//没有任何网络连接
                // TODO Auto-generated method stub
                mLoadingData.dismiss();
            }
        };

    }

    /**
     * 收衣服完成后弹框显示总金额
     * @param order_num
     * @param money
     */
    public void createDialogMoney(final String order_num,final double money) {
        mMenudialog = new Dialog(ApplyclothesinfoActivity.this, R.style.DialogTheme);
        View dialogView = LayoutInflater.from(MainActivity.context).inflate(R.layout.mycommunity_dialog_layout, null);
        mTvTitle = (TextView) dialogView.findViewById(R.id.tv_title);
        mTvTitle.setText("请输入金额：");
        final EditText edittext = (EditText) dialogView.findViewById(R.id.money);
        edittext.setText(money+"");
        btn_ok = (RelativeLayout) dialogView.findViewById(R.id.btn_ok);
        btn_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                show_tv.setText("提交中...");
                mLoadingData.show();
                IOrder.getInstance().PostReceipt(HomeFragment.user_type,order_num, edittext.getText().toString().trim(),postmoneyListener);
                mMenudialog.dismiss();
            }
        });
        dialogView.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mMenudialog.dismiss();
            }
        });
        mMenudialog.setContentView(dialogView);
        mMenudialog.setCancelable(false);
        mMenudialog.setCanceledOnTouchOutside(false);
        mMenudialog.show();
    }
    public static boolean isContain(String s1,String s2) {
        return s1.contains(s2);
    }
}
